package yohoho.xml;

import java.util.List;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import yohoho.model.ServerInfo;

public class ServerListContentHandler extends DefaultHandler {
	private List<ServerInfo> infos=null;
	private ServerInfo serverInfo=null;
	private String tagName=null;
	
	public ServerListContentHandler(List<ServerInfo> infos){
		super();
		this.infos=infos;
	}
	
	public List<ServerInfo> getInfos(){
		return infos;
	}
	
	public void setInfos(List<ServerInfo> infos){
		this.infos=infos;
	}

	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
		// TODO Auto-generated method stub
		String temp=new String(ch,start,length);
		if(tagName.equals("id")){
			serverInfo.setId(temp);
		} else if(tagName.equals("serverName")){
			serverInfo.setServerName(temp);
		} else if(tagName.equals("serverAddress")){
			serverInfo.setServerAddress(temp);
		} else if(tagName.equals("serverUser")){
			serverInfo.setServerUser(temp);
		} else if(tagName.equals("serverPwd")){
			serverInfo.setServerPwd(temp);
		}
				
	}

	@Override
	public void endDocument() throws SAXException {
		// TODO Auto-generated method stub
	}

	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		// TODO Auto-generated method stub
		if(qName.equals("resource")){
			infos.add(serverInfo);
		}
		tagName="";
	}

	@Override
	public void startDocument() throws SAXException {
		// TODO Auto-generated method stub
		super.startDocument();
	}

	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
		// TODO Auto-generated method stub
		this.tagName=localName;
		if(tagName.equals("resource")){
			serverInfo=new ServerInfo();
		}
	}
	
	/*public String readXML(int Str) {
		StringBuffer sb = new StringBuffer();
		String line = null;
		BufferedReader buffer = null;
		//File file=new File
		//Resources r=new Resources().getre
	   // XmlResourceParser xrp = 
	    		//r.getXml(R.xml.serverlist);
		try {
			
			buffer = new BufferedReader(new InputStreamReader(urlConn
					.getInputStream()));
			while ((line = buffer.readLine()) != null) {
				sb.append(line);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				buffer.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return sb.toString();
	}*/
	
}
